﻿using System.Drawing;
using Finance.Data.Entities;
using Finance.Properties;
using TodoSoft;
using TodoSoft.Reporting;

namespace Finance.Reports
{
	public class ProductsReport : Report
	{
		public ProductsReport()
		{
			DataQuery = @"select 
  maindp.mainpid as productid, 
  product.name as productname,
  maindp.maincid as contractorid,
  contractor.name as contractorname,
  maindp.price as price,
  maxdoc.date1,
  maindp.productcategoryid as productcategoryid,
  maindp.productcategoryname as productcategoryname
from (
  select 
    documentproduct.price as price, 
    document.docdatetime as maindocdate, 
    document.contractorid as maincid, 
    documentproduct.productid as mainpid,
    documentsubtype.documentsubtypeid as productcategoryid,
    documentsubtype.name as productcategoryname
  from 
    document 
  inner join
    documentproduct on document.documentid = documentproduct.documentid
  inner join
    documentsubtype on documentsubtype.documentsubtypeid = document.documentsubtypeid
  where
    document.typecode = " + (int)DocumentType.Rashod + @"
)  as maindp
inner join (
  select 
    d1.contractorid as cid1, 
    dp1.productid as dpid1, 
    max(d1.docdatetime) as date1 
  from 
    document as d1 
  inner join 
    documentproduct as dp1 on d1.documentid = dp1.documentid 
  group by 
    d1.contractorid, 
    dp1.productid
) as maxdoc
on 
  maindp.maincid = maxdoc.cid1 and
  maindp.maindocdate = maxdoc.date1 and
  maindp.mainpid = maxdoc.dpid1
inner join 
  product on product.productid = maindp.mainpid
inner join 
  contractor on contractor.contractorid = maindp.maincid
order by 
  productcategoryname, 
  productname,
  price";

			var productCategoryGrouping = new ReportGrouping("productcategoryid");
			productCategoryGrouping.Fields.Add(ReportDataField.First("productcategoryname", 210, ""));
			productCategoryGrouping.HeaderRowVisible = false;
			productCategoryGrouping.Font = new Font("Arial", 10, FontStyle.Bold);
			Groupings.Add(productCategoryGrouping);

			var productGrouping = new ReportGrouping("productid");
			productGrouping.Fields.Add(ReportDataField.First("productname", 120, ""));
			productGrouping.Fields.Add(ReportDataField.Min("price", 50, ""));
			productGrouping.Fields.Add(ReportDataField.Max("price", 50, ""));
			productGrouping.HeaderRowVisible = false;
			Groupings.Add(productGrouping);

			var contractorGrouping = new ReportGrouping("contractorid");
			var contractorNameField = ReportDataField.First("contractorname", 100, Resources.ProductsReport_ProductColumn);
			contractorNameField.Padding = new Margins(15,0,0,0);
			contractorGrouping.Fields.Add(contractorNameField);
			var priceField = ReportDataField.First("price", 100, Resources.ProductsReport_PriceColumn);
			priceField.StringFormat = new StringFormat {Alignment = StringAlignment.Far};
			priceField.FormatArgument = "C";
			contractorGrouping.Fields.Add(priceField);
			contractorGrouping.HeaderRowVisible = true;
			Groupings.Add(contractorGrouping);
		}
	}
}
